define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'statistics/order/index' + location.search,
                    Loss_url: 'statistics/materialplan/index' + location.search + '&type=dashboard',
                    table: 'order',
                },
                pageSize:15,
                paginationDetailHAlign:' hidden',
            });

            var tableOrder = $("#tableOrder");
            var tableLossRate = $("#tableLossRate");


            tableOrder.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                search:false,
                columns: [
                    [
                        {field: 'order_no', title: __('订单号')},
                        {field: 'schedule', title: __('订单进度(%)')}
                    ]
                ]
            });
            tableLossRate.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.Loss_url,
                pk: 'id',
                search:false,
                columns: [
                    [
                        {field: 'trademark_printing', title: __('商标印字')},
                        {field: 'colour.name', title: __('色号')},
                        {field: 'finished_weight', title: __('成品重量')},
                        {field: 'feeding_weight', title: __('投料重量')},
                        {field: 'attrition_rate', title: __('损耗率(%)')}
                    ]
                ],
                onLoadSuccess: function(){ //加载成功时执行
                    var height1=$('#tableOrder').height()+50;
                    var height2=$('#tableLossRate').height()+50;
                    $("#echartOrder").height(height1);
                    $("#echartLossRate").height(height2);
                    echartOrder.resize();
                    echartLossRate.resize();
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(tableOrder);
            Table.api.bindevent(tableLossRate);

            // 基于准备好的dom，初始化echarts实例
            var echartOrder = Echarts.init(document.getElementById('echartOrder'), 'walden');
            var echartLossRate = Echarts.init(document.getElementById('echartLossRate'), 'walden');
            // 指定图表的配置项和数据
            var order_no=Config.statistics.order_no?Config.statistics.order_no:0;

            var start1 = order_no.length>0?order_no.length - 20:0;
            var end1 = order_no.length >0?order_no.length - 1:0;
            var option1={
                title: {
                    text: '订单进度',
                    x:'center',
                    y:'top',
                    textAlign:'center'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '10%',
                    bottom: '20',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    boundaryGap: [0, 0.01],
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        show: false
                    },
                    axisLabel: {
                        formatter:function (value, index) {
                            return value.toFixed(2)+'%';
                        }
                    }
                },
                yAxis: {
                    type: 'category',
                    data: order_no,
                    axisLine: {
                        show: false
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        color: '#999'
                    },
                    splitLine:{
                        show:false
                    }
                },
                dataZoom: [
                    {
                        type: 'slider',
                        show: true,
                        yAxisIndex: [0],
                        right: '5',
                        realtime : true, //拖动时，是否实时更新系列的视图
                        startValue: start1,
                        endValue:end1,
                        zoomOnMouseWheel:false,
                    }
                ],
                series: [
                    {
                        name: '订单进度',
                        type: 'bar',
                        data: Config.statistics.schedule?Config.statistics.schedule:[],
                        showBackground: false,
                        barMaxWidth:35,
                        itemStyle: {
                            color: new Echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: '#83bff6' },
                                { offset: 0.5, color: '#188df0' },
                                { offset: 1, color: '#188df0' }
                            ])
                        },
                        emphasis: {
                            itemStyle: {
                                color: new Echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    { offset: 0, color: '#2378f7' },
                                    { offset: 0.7, color: '#2378f7' },
                                    { offset: 1, color: '#83bff6' }
                                ])
                            }
                        },
                    }
                ]
            };
            var planDataY=Config.materialplan.y?Config.materialplan.y:0;
            var planDataX=Config.materialplan.x?Config.materialplan.x:0;
            var start = planDataY.length>0?planDataY.length - 20:0;
            var end = planDataY.length>0?planDataY.length - 1:0;
            var option2 = {
                title: {
                    text: '损耗率',
                    x:'center',
                    y:'top',
                    textAlign:'center'
                },
                legend:{right:60},
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                grid: {
                    left: '3%',
                    right: '10%',
                    bottom: '20',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    boundaryGap: [0, 0.01],
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        show: false
                    },
                    axisLabel: {
                        formatter:function (value, index) {
                            return value.toFixed(2)+'%';
                        }
                    }
                },
                yAxis: {
                    type: 'category',
                    data: planDataY,
                    axisLine: {
                        show: false
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        color: '#999'
                    },
                    splitLine:{
                        show:false
                    }
                },
                dataZoom: [
                    {
                        type: 'slider',
                        show: true,
                        yAxisIndex: [0],
                        right: '5',
                        realtime : true, //拖动时，是否实时更新系列的视图
                        startValue: start,
                        endValue:end,
                        zoomOnMouseWheel:false,
                    }
                ],
                series: [
                    {
                        name: '损耗率',
                        type: 'bar',
                        data: planDataX,
                        showBackground: false,
                        barMaxWidth:35,
                        itemStyle: {
                            color: new Echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: '#a8a4c1' },
                                { offset: 0.5, color: '#7266ba' },
                                { offset: 1, color: '#7266ba' }
                            ])
                        },
                        emphasis: {
                            itemStyle: {
                                color: new Echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    { offset: 0, color: '#7266ba' },
                                    { offset: 0.7, color: '#7266ba' },
                                    { offset: 1, color: '#83bff6' }
                                ])
                            }
                        },
                    }
                ]
            };
            // 使用刚指定的配置项和数据显示图表。
            echartOrder.setOption(option1);
            echartLossRate.setOption(option2);

            $(window).resize(function () {
                echartOrder.resize();
                echartLossRate.resize();
            });

            $(document).on("click", ".btn-refresh", function () {
                setTimeout(function () {
                    echartOrder.resize();
                    echartLossRate.resize();
                }, 0);
            });



        }
    };

    return Controller;
});
